package cz.najman.emdroid.utils;

import android.content.Context;
import android.content.UriMatcher;
import android.content.res.Resources;
import android.net.Uri;
import cz.najman.emdroid.R;
import cz.najman.emdroid.exception.WrongNumberOfParametersException;
import cz.najman.emdroid.provider.EMDroidDataProvider;

public class LocalToRemoteUriSwitcher implements UriSwitcher {

	private static UriMatcher mUriMatcher = EMDroidDataProvider.mUriMatcher;
	Resources mResources;
	
	public LocalToRemoteUriSwitcher(UriMatcher uriMatcher, Context context) {
		//mUriMatcher = uriMatcher;
		mResources = context.getResources();
	}
	
	
	public Uri switchUri(Uri localUri, String[] parameterTypes, String[] parameterValues) throws WrongNumberOfParametersException {
		
		if(parameterTypes.length != parameterValues.length){
			throw new WrongNumberOfParametersException("LocalToRemoteUriSwitcher: [parameterTypes] and [parameterValues]need to have an equl number of parameters");
		}
		
		//Get the remote uri string with wildcards
		String remoteUri = switchUriInternal(localUri);
						
		// Replace the wildcards in the remoteUri with the
		// runtime parameters
		for (int i = 0; i < parameterTypes.length; i++) {			
			remoteUri = remoteUri.replace(parameterTypes[i], parameterValues[i]);			
		}
						
		// Return the final uri
		return Uri.parse(remoteUri);
	}
	
	/**
	 * 
	 * @param uri uri representing the remote resource
	 * @return String uri representing the remote resource, null if no match found.
	 */
	private String switchUriInternal(Uri uri){		
		
		if(mUriMatcher !=  null){
			
			// Path segment of the remote uri
			// If the uri would be http://host.com/path/to/resouce
			// then the path segment is /path/to/resource
			
			String rmtPathSegment = null;
			
			// Get the base uri specified in the resource files
			String baseUri = mResources.getString(R.string.base_uri);
			
			// Do we recognize the localUri?
			final int match = mUriMatcher.match(uri);
			
			// If we do recognize localUri, swap it for path segment of remote uri
			switch (match) {
			/* EVENTS */
			case EMDroidDataProvider.EVENTS_ID:
				rmtPathSegment = mResources.getString(R.string.remote_uri_events_id);
				break;
			case EMDroidDataProvider.EVENTS_REGISTERED:
				rmtPathSegment = mResources.getString(R.string.remote_uri_events_registered);
				break;
			case EMDroidDataProvider.EVENTS_AVAILABLE:
				rmtPathSegment = mResources.getString(R.string.remote_uri_events_available);
				break;
			case EMDroidDataProvider.EVENTS_MANAGEABLE:
				rmtPathSegment = mResources.getString(R.string.remote_uri_events_managable);
				break;	
				
			/* USERS */
			case EMDroidDataProvider.USERS:
				rmtPathSegment = mResources.getString(R.string.remote_uri_users);
				break;
			case EMDroidDataProvider.USERS_FOR_EVENT:
				rmtPathSegment = mResources.getString(R.string.remote_uri_users_for_event);
				break;
			case EMDroidDataProvider.USERS_FOR_INSTITUTION:
				rmtPathSegment = mResources.getString(R.string.remote_uri_users_for_event);
				break;
			case EMDroidDataProvider.USERS_ID:
				rmtPathSegment = mResources.getString(R.string.remote_uri_users_id);
				break;
			case EMDroidDataProvider.USERS_EMAIL:
				rmtPathSegment = mResources.getString(R.string.remote_uri_users_email);
				break;
				
			/* PAYMENT INFORMATIONS */	
			case EMDroidDataProvider.PAYMENT_INFORMATIONS_ID:
				rmtPathSegment = mResources.getString(R.string.remote_uri_payment_informations_id);
				break;
			case EMDroidDataProvider.PAYMENT_INFORMATIONS:
				rmtPathSegment = mResources.getString(R.string.remote_uri_payment_informations);
				break;
			case EMDroidDataProvider.PAYMENT_VALUES:
				rmtPathSegment = mResources.getString(R.string.remote_uri_payment_values);
				break;
			case EMDroidDataProvider.STORNO_VALUES:
				rmtPathSegment = mResources.getString(R.string.remote_uri_storno_values);
				break;
					
			/* REGISTRATION STATES */	
			case EMDroidDataProvider.REGISTRATION_STATES_ID:
				rmtPathSegment = mResources.getString(R.string.remote_uri_registration_states_id);
				break;
			case EMDroidDataProvider.REGISTRATION_STATES:
				rmtPathSegment = mResources.getString(R.string.remote_uri_registration_states);
				break;
			case EMDroidDataProvider.REGISTRATION_STATES_FOR_USER:
				rmtPathSegment = mResources.getString(R.string.remote_uri_registration_states_for_user);
				break;
			case EMDroidDataProvider.REGISTRATION_STATES_FOR_EVENT:
				rmtPathSegment = mResources.getString(R.string.remote_uri_registration_state_for_event);
				break;
							
			/* EVENT INSTITUTION MANAGERS */
			case EMDroidDataProvider.EVENT_INSTITUTION_MANAGERS_ID:
				rmtPathSegment = mResources.getString(R.string.remote_uri_event_institution_managers_id);
				break;
			case EMDroidDataProvider.EVENT_INSTITUTION_MANAGERS_FOR_USER:
				rmtPathSegment = mResources.getString(R.string.remote_uri_event_institution_managers_for_user);
				break;
			case EMDroidDataProvider.EVENT_INSTITUTION_MANAGERS_FOR_EVENT:
				rmtPathSegment = mResources.getString(R.string.remote_uri_event_institution_managers_for_event);
				break;
			case EMDroidDataProvider.EVENT_INSTITUTION_MANAGERS_FOR_INSTITUTION:	
				rmtPathSegment = mResources.getString(R.string.remote_uri_event_institution_managers_for_institution);
				break;
						
			/* INSTITUTIONS */
			case EMDroidDataProvider.INSTITUTIONS_ID:
				rmtPathSegment = mResources.getString(R.string.remote_uri_institutions_id);
				break;
			case EMDroidDataProvider.INSTITUTIONS:
				rmtPathSegment = mResources.getString(R.string.remote_uri_institutions);
				break;
			case EMDroidDataProvider.INSTITUTIONS_MANAGEABLE_BY_USER:
				rmtPathSegment = mResources.getString(R.string.remote_uri_institutions_manageable);
				break;
			case EMDroidDataProvider.INSTITUTIONS_FOR_EVENT:
				rmtPathSegment = mResources.getString(R.string.remote_uri_institutions_for_event);
				break;
			
			default:
				return null;
				
		}			
			
			if(rmtPathSegment != null){
				// Construct the whole uri from the baseUri and the rmtPathSegment
				StringBuilder builder = new StringBuilder(baseUri);
				builder.append(rmtPathSegment);
				final String remoteUri = builder.toString();
				
				// Return the whole remoteUri string
				// May contain wildcards # for replacing it with runtime values e.g. IDs
				return remoteUri;
			}			
		}
		
		// Return null if no match found
		return null;
	}
	
}
